package ar.edu.unq.tpi.uis.tepe3.wicket.pages;

import java.util.Iterator;
import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;

import org.apache.wicket.extensions.markup.html.tree.table.ColumnLocation;
import org.apache.wicket.extensions.markup.html.tree.table.ColumnLocation.Alignment;
import org.apache.wicket.extensions.markup.html.tree.table.ColumnLocation.Unit;
import org.apache.wicket.extensions.markup.html.tree.table.IColumn;
import org.apache.wicket.extensions.markup.html.tree.table.PropertyRenderableColumn;

import org.apache.wicket.extensions.markup.html.tree.table.TreeTable;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;

import ar.edu.unq.tpi.uis.tepe3.dominio.Folder;
import ar.edu.unq.tpi.uis.tepe3.wicket.AbstractListPage;
import ar.edu.unq.tpi.uis.tepe3.wicket.WicketApplication;
import ar.edu.unq.tpi.uis.tepe3.wicket.extensions.OnlyFolderPropertyTreeColumn;
import ar.edu.unq.tpi.uis.tepe3.wicket.extensions.FolderPropertyTreeTableActionsColumn;
import ar.edu.unq.tpi.uis.tepe3.wicket.panels.DefaultCreateNewMenuPanel;

public class FolderListPage extends AbstractListPage{
	
	private TreeTable tree;
	
	public FolderListPage(){
		super("Lista de carpetas");
		IColumn columns[] = new IColumn[] {
                new OnlyFolderPropertyTreeColumn(new ColumnLocation(Alignment.MIDDLE, 8, Unit.PROPORTIONAL), "Arbol", "userObject.getName()"),
                new PropertyRenderableColumn(new ColumnLocation(Alignment.MIDDLE, 8, Unit.PROPORTIONAL), "Path Completo", "userObject.getPath()"),
                new PropertyRenderableColumn(new ColumnLocation(Alignment.MIDDLE, 8, Unit.PROPORTIONAL), "Cantidad de permisos", "userObject.getNoOfAccesses()"),
                new FolderPropertyTreeTableActionsColumn(new ColumnLocation(Alignment.MIDDLE, 8, Unit.PROPORTIONAL), "Acciones", ""),
		};

        tree = new TreeTable("treeTable", createTreeModel(), columns);
        add(tree);
	}
	
	/**
     * Creates the model that feeds the tree.
     * 
     * @return New instance of tree model.
     */
    protected TreeModel createTreeModel()
    {
        return convertToTreeModel(((WicketApplication)this.getApplication()).getModel().getFileSystem());
    }

    private TreeModel convertToTreeModel(Folder aFolder)
    {
        TreeModel model = null;
        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(aFolder);
        add(rootNode, aFolder.getChildren());
        model = new DefaultTreeModel(rootNode);
        return model;
    }

    private void add(DefaultMutableTreeNode parent, List<Folder> sub)
    {
        for (Iterator<Folder> i = sub.iterator(); i.hasNext();)
        {	
            Folder o = i.next();
            if (o instanceof List<?>)
            {
                List<Folder> children  = (List)o;
            	DefaultMutableTreeNode child = new DefaultMutableTreeNode(new Folder("subtree"));
                parent.add(child);
                add(child, children);
            }
            else
            {
                DefaultMutableTreeNode child = new DefaultMutableTreeNode(o);
                parent.add(child);
                add(child, o.getChildren());
            }
        }
    }

	@Override
	public void deleteAction(IModel model) {
		
		try {
			((WicketApplication) getApplication()).getModel().getFileSystem().removeChild((Folder) ((DefaultMutableTreeNode) getModel().getObject()).getUserObject());
			((WicketApplication) getApplication()).save();
//			deleteSucceed();
		} catch (Exception e) {
			//warn(e.getMessage());
			e.printStackTrace();
		}	
	}

	@Override
	protected void addLeftMenu() {
		add(new DefaultCreateNewMenuPanel("leftMenu", new CompoundPropertyModel(new Folder(""))));
	}
    
}
